package com.wjgl.service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.wjgl.dao.DepartmentDAO;
import com.wjgl.dao.InstitutionDAO;
import com.wjgl.dao.RoleDAO;
import com.wjgl.dao.UserDAO;
import com.wjgl.domain.Department;
import com.wjgl.domain.Institution;
import com.wjgl.domain.Role;
import com.wjgl.domain.User;

public class UserService {
	private UserDAO userDAO;
	private InstitutionDAO institutionDAO;
	private DepartmentDAO departmentDAO;
	private RoleDAO roleDAO;
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	public void setInstitutionDAO(InstitutionDAO institutionDAO) {
		this.institutionDAO = institutionDAO;
	}

	public void setDepartmentDAO(DepartmentDAO departmentDAO) {
		this.departmentDAO = departmentDAO;
	}

	public void setRoleDAO(RoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}

	public User login(User user){
		List<User> list = new ArrayList<User>();
		User u = null;
		list = userDAO.findByName(user.getName());
		if(list.size() > 0)
			u = list.get(0);
		if(u != null)
			if(u.getName().equals(user.getName()) && u.getPassword().equals(user.getPassword())){
				Date date = new Date();
				//System.out.println(date);
				SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String time = df1.format(date);
				//System.out.println(time);
				u.setLogindate(Timestamp.valueOf(time));
				userDAO.update(u);
				return u;
			}
		return null;
	}
	
	public User findById(Integer id){
		return userDAO.findById(id);
	}
	
	public List<User> getAllUsers(){
			return (List<User>)userDAO.findAll();
	}
	
	public boolean lockUser(int id){
		User u = userDAO.findById(id);
		boolean b = true;
		if(u == null)
			b = false;
		else{
			u.setState((short) 2);
			userDAO.update(u);
		}
		return b;
	}
	
	public boolean unlockUser(int id){
		User u = userDAO.findById(id);
		boolean b = true;
		if(u == null)
			b = false;
		else{
			u.setState((short) 1);
			userDAO.update(u);
		}
		return b;
	}
	
	public boolean activateUser(int id){
		User u = userDAO.findById(id);
		boolean b = true;
		if(u == null)
			b = false;
		else{
			u.setState((short) 1);
			userDAO.update(u);
		}
		return b;
	}
	
	
	public boolean deleteUser(int id){
		User u = userDAO.findById(id);
		boolean b = false;
		if(u != null){
			userDAO.delete(u);
			b = true;
		}
		return b;
	}
	
	public boolean allocateUser(int id, int institutionId, int departmentId, int roleId){
		User u = userDAO.findById(id);
		boolean b = false;
		if(u != null){
			Institution ins = institutionDAO.findById(institutionId);
			if(ins != null){
			//	System.out.println("insname="+ins.getName());
				u.setInstitution(ins);
				Department d = departmentDAO.findById(departmentId);
				if(d != null){
				//	System.out.println("depname="+d.getName());
					u.setDepartment(d);
				}
				
				Role role = roleDAO.findById(roleId);
				if(role != null){
				//	System.out.println("rolername="+role.getName());
					if(role.getId() == 1){
						u.setIsadmin((short)1);
						ins.setAdminId(u.getId());
					}
					else{
						u.setIsadmin((short)0);
						if(ins.getAdminId() == u.getId())
							ins.setAdminId(0);
					}
					institutionDAO.update(ins);
					u.setRole(role);
				}			
			}
			userDAO.update(u);
			b=true;
		}
		return b;
	}
	
	public void register(User user){
		userDAO.save(user);
	}

}
